package com.itheima.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dubbo.api.BlackListApi;
import com.itheima.dubbo.api.QuestionApi;
import com.itheima.dubbo.api.SettingsApi;
import com.itheima.server.interceptor.UserHolder;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;


    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1. 获取当前登录的用户的
        User user = UserHolder.get();
        //2. 查询该用户的陌生人问题
        Question question = questionApi.findByUserId(user.getId());
        vo.setStrangerQuestion(question==null?"你有什么兴趣呢":question.getTxt());

        //设置手机号
        vo.setPhone(user.getMobile());

        //3. 询该用户通知设置
        Settings settings = settingsApi.findByUserId(user.getId());
        if(settings!=null) {
            vo.setId(settings.getId());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        return vo;
    }

    /**
     * 作用：新增或者更新用户陌生人问题
     * @param content
     */
    public void saveQuestion(String content) {
        //1. 获取当前登录的用户id
        Long userId = UserHolder.getId();

        //2. 根据用户的id查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);

        //3. 判断陌生人是否为null，为null则新增、不为null则修改
        if(question==null){
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);

        }else{
            question.setTxt(content);
            question.setUpdated(null);
            questionApi.update(question);
        }

    }

    /**
     * 查询黑名单列表
     * @param page
     * @param pagesize
     * @return
     *
     * mybatis-plus的分页方法的要求：
     *      1. 形参列表必须有一个Page对象
     *      2. 返回值也需要有一个Page对象。
     */
    public PageResult queryBlackList(Integer page, Integer pagesize) {
        //1. 获取当前登录的用户的id
        Long userId = UserHolder.getId();

        //2. 根据用户的id查询该用户黑名单分页对象  Page对象  Page<UserInfo>
        Page<UserInfo> pageInfo  = blackListApi.queryBlackList(page,pagesize,userId);



        //3. 创建PageResult对象，把Page对象的信息转移到PageResult对象中
        /* PageResult pageResult = new PageResult(((Long)pageInfo.getCurrent()).intValue(),((Long)pageInfo.getSize()).intValue(),
           ((Long)pageInfo.getTotal()).intValue(),pageInfo.getRecords()); */
        PageResult pageResult = new PageResult(pageInfo.getCurrent(),pageInfo.getSize(),pageInfo.getTotal(),pageInfo.getRecords());

        return pageResult;

    }

    /**
     * 接口名称：移除黑名单
     * @param blackUserId
     */
    public void removeBlackUser(Long blackUserId) {
        //1. 得到当前登陆者id
        Long userId = UserHolder.getId();
        //2. 删除黑名单
        blackListApi.removeBlackUser(userId,blackUserId);
    }
}
